package xyz.renyumeng.xkm.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import xyz.renyumeng.xkm.model.user.UserInfo;
import xyz.renyumeng.xkm.result.Result;
import xyz.renyumeng.xkm.user.service.UserInfoService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author renyumeng
 * @date Created in 16:23 2023/6/29
 */


@Api(tags = "管理员用户信息接口")
@RestController
@RequestMapping("/admin/user/user-info")
public class AdminUserInfoController {


    @Resource
    private UserInfoService userInfoService;


    @ApiOperation("获取用户列表")
    @GetMapping("/list-user-info")
    public Result<List<UserInfo>> ListUserInfo() {
        List<UserInfo> list = userInfoService.list();
        return Result.ok(list);
    }


    @ApiOperation("根据id获取用户信息")
    @GetMapping("/{id}")
    public Result<UserInfo> getUserById(@PathVariable Long id) {
        return userInfoService.getUserInfoByIdWithAddressInfo(id);

    }

    @ApiOperation("内部接口根据id获取用户信息")
    @GetMapping("/inner/{id}")
    public UserInfo innerGetUserByIdWithAddressInfo(@PathVariable Long id) {
        return userInfoService.innerGetUserInfoByIdWithAddressInfo(id);
    }

    @PostMapping
    @ApiOperation("根据id增加用户信息")
    public Result<Object> addUserById(@RequestBody UserInfo userInfo) {
        return userInfoService.addUserById(userInfo);

    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除用户信息")
    public Result<Object> deleteUserById(@PathVariable Long id) {
        return userInfoService.deleteUserById(id);
    }

    @PutMapping
    @ApiOperation("根据id修改用户信息")
    public Result<Object> updateUserById(@RequestBody UserInfo userInfo) {
        boolean result = userInfoService.updateById(userInfo);
        if (!result) {
            return Result.fail().message("用户不存在!");
        }
        return Result.ok().message("修改成功!");
    }
}
